<?php

/* define initial database server  */
include_once('conf/dbconfig.php');
$cfg = new dbconfig();
$host = $cfg->get_dbhost();
$dbname = $cfg->get_dbname();
$user = $cfg->get_dbusername();
$pass = $cfg->get_dbpwd();
/* connect to server  */
$connect = pg_connect("host = $host dbname = $dbname user = $user password = $pass")
        or die("Cannot connect to server");

$usr = $_SESSION['user'];
$query = "select * from customer where username = '$usr'";
$result = pg_query($query);

if (isset($_POST['change'])) {
    $id = pg_fetch_result($result, 0, 'customerid');
    $new_email = $_POST['email'];
    $new_acc = $_POST['accno'];
    $new_bank = $_POST['bank'];
    $new_phone = $_POST['phone'];
    $new_occu = $_POST['occupation'];
    $new_add = $_POST['address'];

    $query = "update customer set email = '$new_email', accno = '$new_acc', phonenumber = '$new_phone', occupation = '$new_occu', address = '$new_add'";
    if ($new_bank != -1)
        $query .= ", banks = '$new_bank'";
    $query .= " where customerid = '$id'";
    $check = pg_query($connect, $query);
    if (pg_affected_rows($check) > 0) {
        echo "<h5>**Thay đổi của bạn đã được thực hiện</h5>";
        $query = "select * from customer where username = '$usr'";
        $result = pg_query($query);
    }
}
?>